package com.yz.lzq.liwushuo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.yz.lzq.liwushuo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SearchActivity extends AppCompatActivity {


    /**
     * 标题
     */

    @Bind(R.id.edittext_search)
    //搜索文本编辑框
            EditText edittextSearch;

    @Bind(R.id.textVeiw_search_cancel)
    //右取消返回键
            TextView textVeiwSearchCancel;
    //搜索后
    @Bind(R.id.button_search_back)
    //    左返回键
            Button buttonSearchBack;
    /**
     * 主体内容
     */
    //大家都在搜前的图片
    @Bind(R.id.image_holtSearch_show)
    ImageView imageHoltSearchShow;
    //大家都在搜热词
    @Bind(R.id.gridView_holtSearch_show)
    GridView gridViewHoltSearchShow;
    //历史搜索头部图标
    @Bind(R.id.image_Search_show)
    ImageView imageSearchShow;
    //历史搜索头部删除全部图标
    @Bind(R.id.image_Search_delete)
    ImageView imageSearchDelete;
    //历史搜索布局
    @Bind(R.id.relativeLayout_searchHestory)
    RelativeLayout relativeLayoutSearchHestory;
    //历史搜索
    @Bind(R.id.listView_search)
    ListView listViewSearch;
    //搜索前引入布局
    @Bind(R.id.search_frist_view)
    ScrollView searchFristView;
    @Bind(R.id.search_after_view)
    //编辑后显示的引入布局
            LinearLayout searchAfterView;
    //本界面最外层布局id
    @Bind(R.id.activity_search)
    LinearLayout activitySearch;
    //搜索后
    @Bind(R.id.search_after_over)
    LinearLayout searchAfterOver;
    @Bind(R.id.textView_search_hide)
    TextView textViewSearchHide;
//    @Bind(R.id.recyclerView_search_gongL)
//    RecyclerView recyclerViewSearchGongL;
//    @Bind(R.id.recyclerView_search_danP)
//    RecyclerView recyclerViewSearchDanP;

    //    private EditText editText_search;
//    private ScrollView search_frist_view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉系统自带的标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        //搜索框控件

        //被隐藏的控件
        // hideSv = (ScrollView) findViewById(R.id.search_after_view);
        //被隐藏的控件


        //et这个控件对象的三个监听：触摸监听、内容改动监听、键盘回车进行搜索的监听
        //搜索框的触摸监听，当触摸时，hint的提示文本内容置为空
        edittextSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                //把EditText内容置空
                edittextSearch.setHint("");
                return false;
            }

        });
        /*搜索框内容监听*/
        edittextSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //图像所在的页面消失
                searchFristView.setVisibility(View.GONE);
                //搜索内容所在的页面出现
                searchAfterView.setVisibility(View.VISIBLE);

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        /* 搜索框的另一个key监听，作用是输入完内容时，点击软键盘的回车功能时，
        *  隐藏软键盘，并得到搜索框内的搜索关键词
        */
        edittextSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (keyCode == keyEvent.KEYCODE_ENTER) {
                    //得到搜索文本
                    String searchText = edittextSearch.getText().toString().trim();
                    //隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(SearchActivity
                            .INPUT_METHOD_SERVICE);
                    //view.getWindowToken()
                    // 获取调用的view依附在哪个window的令牌
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    //让et失去焦点
                    edittextSearch.clearFocus();
                    searchAfterView.setVisibility(View.GONE);
                    textViewSearchHide.setVisibility(View.GONE);
                    searchAfterOver.setVisibility(View.VISIBLE);

                    //这里是可以展示内容的部分，我用了一个toast来让拿到搜索内容的目的实现
                    Toast.makeText(SearchActivity.this, "您搜索了：" + searchText, Toast.LENGTH_SHORT)
                            .show();
                }
                return false;
            }
        });
    }

    //取消按钮，功能是关闭搜索这个页面
    public void cancel(View view) {
        finish();
    }
}

